import React from 'react'
import { Route, Router, Switch } from 'dva/router'
import { getRouterData, getMenuRouteArr, getMenuData } from './menu'

function RouterConfig({ history, app }) {

  // 将菜单及其组件保存到global中
  const routerData = getRouterData(app)
  const menuRouteArr = getMenuRouteArr(routerData)
  app._store.dispatch({
    type: 'menu/saveMenuRouteArr',
    payload: menuRouteArr,
  })

  // 保存菜单数据
  const menuData = getMenuData(routerData)
  app._store.dispatch({
    type: 'menu/saveMenuData',
    payload: menuData,
  })

  // 从浏览器本地存储读取语言配置信息并保存到global中
  const language = localStorage.getItem('language')
  app._store.dispatch({
    type: 'global/saveLanguage',
    payload: language,
  })

  const BasicLayout = routerData['/'].component
  const Login = routerData['/login'].component

  return (
    <Router history={history}>
      <Switch>
        <Route strict path="/login" component={Login} />
        <Route path="/" component={BasicLayout} />
      </Switch>
    </Router>
  )
}

export default RouterConfig
